/*
 * Copyright (c) [2022] [王鹏欢]
 *    [DataStructure] is licensed under Mulan PSL v2.
 *    You can use this software according to the terms and conditions of the Mulan PSL v2.
 *    You may obtain a copy of Mulan PSL v2 at:
 *             http://license.coscl.org.cn/MulanPSL2
 *    THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 *    See the Mulan PSL v2 for more details.
 */

package com.huan.sequence;

import java.util.*;


public class StudentManageSystem extends SequenceList {

    /**
     * 按顺序构造顺序表
     * @param maxsize 顺序表的最大存储空间容量
     * @param n 创建含有n个数据元素的顺序表
     * @throws Exception
     */
    public StudentManageSystem(int maxsize, int n) throws Exception
    {
        super(maxsize);   //调用父类构造函数  
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < n; i++) {     //创建含有n个数据元素的顺序表
            StudentNode node = new StudentNode(sc);
            insert(node);      //将新的数据元素插入到顺序表的表尾  
        }
    }

    /**
     * 重载父类insert（）方法在顺序表表尾插入一个学生信息
     *
     * @param node
     * @throws Exception
     */
    public void insert(StudentNode node) throws Exception
    {
        super.insert(length(), node);
    }

    /**
     * 覆盖父类get（）方法，
     * @param number 从顺序表中取出指定学号的学生信息，
     * @return 返回StudentNode对象
     * @throws Exception
     */
    public StudentNode get(int number) throws Exception
    {
        for (int i = 0; i < length(); i++) {   //遍历顺序表
            StudentNode node = (StudentNode) super.get(i);  //调用父类SequenceList的get方法
            if (node.number == number)
                return node;               //返回指定学生信息  
        }
        throw new Exception("学号" + number + "不存在");
    }


    /**
     * 覆盖父类的remove方法
     *
     * @param number
     * @throws Exception
     */
    public void remove(int number) throws Exception
    {
        for (int i = 0; i < length(); i++) {
            StudentNode node = (StudentNode) super.get(i);
            if (node.number == number)
                super.remove(i);
            return;           //提前退出函数  
        }
        throw new Exception("学号" + number + "不存在");
    }

    /**
     * 重载父类display方法，输出顺序表中的所有数据元素
     */
    public void display()
    {
        for (int i = 0; i < length(); i++) {
            try {
                StudentNode node = (StudentNode) super.get(i);
                displayNode(node);
            }
            catch (Exception e) {
            }
        }
    }


    /**
     * 输出一个数据元素的信息
     * @param node
     */
    public void displayNode(StudentNode node)
    {
        System.out.println("学号：" + node.number + "姓名：" + node.name + "性别：" + node.sex + "英语成绩：" +
                                   node.english + "数学成绩：" + node.math);
    }

    public static void main(String[] args) throws Exception
    {
        int maxsize = 100;
        Scanner sc = new Scanner(System.in);
        System.out.println("输入学生总数");
        int n = sc.nextInt();
        System.out.println("请按学号，姓名，性别，英语成绩，数学成绩顺序输入学生信息");
        StudentManageSystem L = new StudentManageSystem(maxsize, n);
        System.out.println("输出学生信息");
        L.display();
        System.out.println("输入要查询学生的学号");
        L.displayNode(L.get(sc.nextInt()));
        System.out.println("输入要删除学生的学号");
        L.remove(sc.nextInt());
        System.out.println("删除成功");
        L.display();
        System.out.println("输入需要增加的学生信息");
        L.insert(new StudentNode(sc));
        L.display();
    }
}
 